<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="telephone">
        <el-input
          v-model="queryParams.telephone"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="真实名称" prop="trueName">
        <el-input
          v-model="queryParams.trueName"
          placeholder="请输入真实名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['member:member:add']"-->
<!--        >新增-->
<!--        </el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="memberList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="序号" align="center" prop="memberId"/>
      <el-table-column label="名称" align="center" prop="userName"/>
      <el-table-column label="权限级别" align="center" prop="vipName"/>
      <el-table-column label="权限期限" align="center" prop="maturityTime"/>
      <el-table-column label="最后登录时间" align="center" prop="lastVist"/>
      <el-table-column label="最后登录ip" align="center" prop="lastIp"/>
      <el-table-column label="手机号" align="center" prop="telephone"/>
      <el-table-column label="真实名称" align="center" prop="trueName"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['member:member:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['member:member:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改会员信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="名称" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入名称"/>
        </el-form-item>
        <el-form-item label="总空间" prop="totalSpace">
          <el-input v-model="form.totalSpace" placeholder="请输入总空间"/>
        </el-form-item>
        <el-form-item label="生日" prop="bday">
          <el-input v-model="form.bday" placeholder="请输入生日"/>
        </el-form-item>
        <el-form-item label="icon" prop="icon">
          <image-upload v-model="form.icon"/>
        </el-form-item>
        <el-form-item label="个人简介" prop="introduce">
          <el-input v-model="form.introduce" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="oicq" prop="oicq">
          <el-input v-model="form.oicq" placeholder="请输入oicq"/>
        </el-form-item>
        <el-form-item label="msn" prop="msn">
          <el-input v-model="form.msn" placeholder="请输入msn"/>
        </el-form-item>
        <el-form-item label="首页" prop="homepage">
          <el-input v-model="form.homepage" placeholder="请输入首页"/>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱"/>
        </el-form-item>
        <el-form-item label="省" prop="province">
          <el-select v-model="form.province" filterable @change="provinceChange" placeholder="请选择省">
            <el-option
              v-for="item in provinceOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="市" prop="city">
          <el-select v-model="form.city" filterable placeholder="请选择市">
            <el-option
              v-for="item in cityOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入详细地址"/>
        </el-form-item>
        <el-form-item label="联系方式" prop="mobPhone">
          <el-input v-model="form.mobPhone" placeholder="请输入联系方式"/>
        </el-form-item>
        <el-form-item label="手机号" prop="telephone">
          <el-input v-model="form.telephone" placeholder="请输入手机号"/>
        </el-form-item>
        <el-form-item label="身份证号" prop="idCard">
          <el-input v-model="form.idCard" placeholder="请输入身份证号"/>
        </el-form-item>
        <el-form-item label="真实名称" prop="trueName">
          <el-input v-model="form.trueName" placeholder="请输入真实名称"/>
        </el-form-item>
        <el-form-item label="是否邮箱验证" prop="emailVerify">
          <el-radio-group v-model="form.emailVerify">
            <el-radio
              v-for="dict in verifyOptions"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否手机验证" prop="mobVerify">
          <el-radio-group v-model="form.mobVerify">
            <el-radio
              v-for="dict in verifyOptions"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="身份证审核状态" prop="idCardVerify">
          <el-radio-group v-model="form.idCardVerify">
            <el-radio
              v-for="dict in audioOptions"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
<!--        <el-form-item label="微信openId" prop="wechatOpenId">-->
<!--          <el-input v-model="form.wechatOpenId" type="textarea" placeholder="请输入内容"/>-->
<!--        </el-form-item>-->
        <el-form-item label="背景图" prop="backgroundImg">
          <image-upload v-model="form.backgroundImg"/>
        </el-form-item>
        <el-form-item label="头像" prop="userImg">
          <image-upload v-model="form.userImg"/>
        </el-form-item>
        <el-form-item label="昵称" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入昵称"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listMember, getMember, delMember, addMember, updateMember} from "@/api/member/member";
import {listArea} from "@/api/area/area";

export default {
  name: "Member",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 会员信息表格数据
      memberList: [],
      verifyOptions: [{value: 0, label: '未验证'}, {value: 1, label: '已验证'}],
      audioOptions: [{value: 0, label: '未审核'}, {value: 1, label: '已审核'}, {value: 2, label: '等待审核'}],
      provinceOptions: [],
      cityOptions:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: null,
        telephone: null,
        trueName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
    this.getProvinceList();
    this.getCityList({"grade": "2"});
  },
  methods: {
    /** 查询会员信息列表 */
    getList() {
      this.loading = true;
      listMember(this.queryParams).then(response => {
        this.memberList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询省列表 */
    getProvinceList() {
      listArea({"grade": "1"}).then(response => {
        this.provinceOptions = response.data;
      });
    },
    /** 查询市列表 */
    getCityList(query) {
      listArea(query).then(response => {
        this.cityOptions = response.data;
      });
    },
    provinceChange(item) {
      this.getCityList({"parentId":item});
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        memberId: null,
        userName: null,
        status: null,
        score: null,
        totalSpace: null,
        useSpace: null,
        olTime: null,
        lastVist: null,
        lastIp: null,
        regDate: null,
        regIp: null,
        sex: null,
        bday: null,
        icon: null,
        introduce: null,
        oicq: null,
        msn: null,
        homepage: null,
        email: null,
        province: null,
        city: null,
        address: null,
        mobPhone: null,
        telephone: null,
        idCard: null,
        trueName: null,
        emailVerify: null,
        mobVerify: null,
        idCardVerify: null,
        wechatOpenId: null,
        backgroundImg: null,
        website: null,
        lastPlan: null,
        userImg: null,
        nickName: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.memberId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加会员信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const memberId = row.memberId || this.ids
      getMember(memberId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改会员信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.memberId != null) {
            updateMember(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMember(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const memberIds = row.memberId || this.ids;
      this.$modal.confirm('是否确认删除会员信息编号为"' + memberIds + '"的数据项？').then(function () {
        return delMember(memberIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('member/member/export', {
        ...this.queryParams
      }, `member_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
